﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

using RabbitMQ.Client;
using System.Threading;

namespace EmitLogDirect
{
    class Program
    {
        static void Main(string[] args)
        {
            var factory = new ConnectionFactory()
            {
                HostName = "localhost"
            };


            using (var connection=factory.CreateConnection())
            using (var channel = connection.CreateModel())
            {
                //声明通道
                channel.ExchangeDeclare(
                    exchange: "direct_logs",
                    type: "direct");
                var queueName = channel.QueueDeclare().QueueName;


                //消息
                int number = 0;
                while(true)
                {
                    Thread.Sleep(500);
                    number++;
                    //var message = Console.ReadLine();                    
                    string[] errorType={"info","error","warn"};
                    Random random = new Random();
                    int index = random.Next(0, errorType.Length - 1);
                    var message = errorType[index] + "," + number.ToString();
                    var messages = message.Split(',');

                    var body = Encoding.UTF8.GetBytes(message);

                    channel.BasicPublish(exchange: "direct_logs",
                        routingKey: messages[0],
                        basicProperties: null,
                        body: body);
                    Console.WriteLine(" Send Message : " + message);
                }

            }
        }
    }
}
